<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class Usercoupon extends Base
{

    protected $name = 'user_coupon';

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }
    //领取优惠券
    public static  function couponAdd($param, $user)
    {
        parent::startTrans();
        try {
            $find = parent::where(['coupon_id' => $param['id'], 'uid' => $user->id])->find();
            $coupon = Coupon::where(['id' => $param['id']])->find();
            if ($coupon['types'] != 3) {
                if (!empty($find) && $coupon['receive'] == 1) {
                    return ['code' => -1, 'msg' => "您已领取该优惠券！"];
                }
                if ($coupon['types'] == 2) {
                    $finds = parent::where(['coupon_id' => $param['id'], 'order_id' => $param['order_id'], 'uid' => $user->id])->find();
                    if (!empty($finds)) {
                        return ['code' => -1, 'msg' => "您已领取该优惠券！"];
                    }
                    $data['order_id'] = $param['order_id'];
                }
            }else{
                return ['code' => -1, 'msg' => "该优惠券不可领取！"];
            }
            $data['title'] = $coupon['title'];
            $data['money'] = $coupon['money'];
            $data['sale_money'] = $coupon['sale_money'];
            $data['end_time'] = $coupon['time'] * 86400 + time();
            $data['coupon_id'] = $param['id'];
            $data['uid'] = $user['id'];
            $data['create_time'] = time();
            parent::create($data);
            $finds = Coupon::where(['id' => $param['id'], 'is_del' => 0, 'status' => 1])->find();
            if (!empty($finds)) {
                if ($finds['num'] == 1) {
                    Coupon::where(['id' => $param['id'], 'is_del' => 0])->update(['status' => 0]);
                }
                if ($finds['num'] > 0) {
                    Coupon::where('id', $param['id'])->dec('num', 1)->update();
                }
            }
            parent::commit();
            return ['code' => 1, 'msg' => "领取成功！"];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    //领取优惠券
    public static  function couponAllAdd($user)
    {
        parent::startTrans();
        try {
            
            $map[] = ['level_ids', 'like', '%' . $user->level_id . '%'];
            $lsit = Coupon::where(['is_del' => 0, 'status' => 1,'types'=>1])->where($map)->select();
            foreach ($lsit as $k => $v) {
                $find = parent::where(['coupon_id' => $v['id'], 'uid' => $user->id,'is_del'=>0])->find();
                if (empty($find)) {
                    $data['title'] = $v['title'];
                    $data['money'] = $v['money'];
                    $data['sale_money'] = $v['sale_money'];
                    $data['end_time'] = $v['time'] * 86400 + time();
                    $data['coupon_id'] = $v['id'];
                    $data['uid'] = $user['id'];
                    $data['create_time'] = time();
                    $dataAll[] = $data;
                }
            }
            if (!empty($dataAll)) {
                parent::insertAll($dataAll);
                foreach ($dataAll as $kk => $vv) {
                    $finds = Coupon::where(['id' => $vv['coupon_id'], 'is_del' => 0, 'status' => 1])->find();
                    if (!empty($find)) {
                        if ($finds['num'] == 1) {
                            Coupon::where(['id' => $vv['coupon_id'], 'is_del' => 0])->update(['status' => 0]);
                        }
                        if ($finds['num'] > 0) {
                            Coupon::where('id', $vv['coupon_id'])->dec('num', 1)->update();
                        }
                    }
                }
            }
            parent::commit();
            return ['code' => 1, 'msg' => "领取成功！"];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
